﻿using LuckyShell.ShellOrders;

namespace LuckyShell.ShellValues;

public class InstantiateValue(string name, List<ShellValue> valueList) : ShellValue
{
    public object GetValue(ref VariateManager manager)
    {
        var func = manager.GetValue(Name);
        var _m = new VariateManager();
        if (func.Arg.Count != ValueList.Count)
            throw new Exception($"Arg count({func.Arg.Count}) is different to value's count ({ValueList.Count})");
        for (var i = 0; i < func.Arg.Count; i++)
        {
            var o = ValueList[i].GetValue(ref manager);
            _m.SetValue(func.Arg[i], o);
        }

        func.BlockOrder.Run(ref _m);
        return func.Return == null ? new VoidValue() : func.Return.GetValue(ref _m);
    }

    public string Name { get; set; } = name;
    public List<ShellValue> ValueList { get; set; } = valueList;
}